package cn.b.sky.mes.hr.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.b.sky.mes.hr.model.condition.PerformanceCondition;
import cn.b.sky.mes.hr.service.WorkCenterPerformanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.cm.service.WorkCenterService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.common.utils.DateUtils;
import cn.b.sky.user.controller.SecurityController;

/**
 * 
 * <strong>Title : WorkCenterPerformanceAnalysisController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.hr.controller<br></strong>
 * <strong>Description : </strong>工作中心人员绩效分析<br> 
 * <strong>Create on : 2016-3-31 下午4:21:16<br></strong>
 * <p> 
 * @author ZiQiang.Zhao<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/hr_workCenterPerformance/*")
@Slf4j
public class WorkCenterPerformanceAnalysisController extends SecurityController {

  @Autowired
  private WorkCenterPerformanceService workCenterPerformanceService;

  @Autowired
  private WorkCenterService workCenterService;

  @RequestMapping("index")
  public String performanceStatistics(String navTabId, PerformanceCondition condition, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    condition.setSiteCode(getSiteCode());
    String siteCode = getSiteCode();
    String materialCode = condition.getMaterialCode();

    workCenterPerformanceService.initDateAndWorkCenterCode(condition);
    List<String> dateArray = null;
    try {
      dateArray = DateUtils.getDateUtils().getDatesBetweenTwoDate(condition.getMinCreateDateTime(),
          condition.getMaxCreateDateTime());
    } catch (ParseException e) {
      //   return ajaxError(getImeViewMessageI18n(null,SystemConstant.PARAMETERNULL));
      log.error("{}", e);
    }
    String[] textArray = {};
    if (condition.getWorkCenterCode() != null && !condition.getWorkCenterCode().isEmpty()) {
      textArray = condition.getWorkCenterCode().split(",");
    }
    List<List<Double>> dataArray = new ArrayList<List<Double>>();
    List<PerformanceCondition> conditionList = new ArrayList<PerformanceCondition>();
    if (dateArray != null && !dateArray.isEmpty()) {
      for (String workCenterCode : textArray) {
        List<Double> ratioData = new ArrayList<Double>();
        for (String date : dateArray) {
          ratioData.add(workCenterPerformanceService.workCenterInOutRatioSomeDay(siteCode, workCenterCode, date,
              materialCode));
        }
        dataArray.add(ratioData);
      }

      for (String date : dateArray) {
        //    List<Double> ratioData = new ArrayList<Double>();
        for (String workCenterCode : textArray) {
          PerformanceCondition obj = new PerformanceCondition();
          obj.setTheDate(date);
          obj.setWorkCenterCode(workCenterCode);
          //    obj.setWorkcenterText(workcenterText);
          conditionList.add(obj);
        }
      }
    }

    List<PerformanceCondition> dataList = conditionList;
    int count = conditionList.size();
    if (count > 0) {
      int endRow = condition.getEndRow();
      if (endRow > count) {
        endRow = count;
      }
      dataList = conditionList.subList(condition.getStartRow() - 1, endRow);
    }

    for (PerformanceCondition tmp : dataList) {
      Double ratio = workCenterPerformanceService.workCenterInOutRatioSomeDay(siteCode, tmp.getWorkCenterCode(),
          tmp.getTheDate(), materialCode);
      tmp.setRatio(new BigDecimal(ratio).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
      WorkCenter workCenter = workCenterService.getWorkCenterByPK(siteCode, tmp.getWorkCenterCode());
      String workCenterText = "";
      if (workCenter != null) {
        workCenterText = workCenter.getWorkCenterText();
      }
      tmp.setWorkcenterText(workCenterText);
    }

    condition.setData(dataList);
    condition.setTotalCount(count);

    model.addAttribute("page", condition);
    return "hr/performance/workCenterPerf_analysis";
  }

  @RequestMapping("performanceDataForLine")
  @ResponseBody
  public DWZResult performanceDataForLine(String navTabId, PerformanceCondition condition) {
    condition.setSiteCode(getSiteCode());
    String siteCode = getSiteCode();
    HashMap<String, Object> chartData = new HashMap<String, Object>();

    workCenterPerformanceService.initDateAndWorkCenterCode(condition);
    List<String> dateArray;
    try {
      dateArray = DateUtils.getDateUtils().getDatesBetweenTwoDate(condition.getMinCreateDateTime(),
          condition.getMaxCreateDateTime());
    } catch (ParseException e) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String[] textArray = {};
    if (condition.getWorkCenterCode() != null && !condition.getWorkCenterCode().isEmpty()) {
      textArray = condition.getWorkCenterCode().split(",");
    }
    List<String> workCenterTextArray = new ArrayList<String>(textArray.length);
    List<List<Double>> dataArray = new ArrayList<List<Double>>();
    for (String workCenterCode : textArray) {
      List<Double> ratioData = new ArrayList<Double>();
      for (String date : dateArray) {

        Double ratio = workCenterPerformanceService.workCenterInOutRatioSomeDay(siteCode, workCenterCode, date,
            condition.getMaterialCode());
        ratioData.add(new BigDecimal(ratio).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
      }
      dataArray.add(ratioData);
      WorkCenter workCenter = workCenterService.getWorkCenterByPK(siteCode, workCenterCode);
      String workCenterText = "";
      if (workCenter != null) {
        workCenterText = workCenter.getWorkCenterText();
      }
      workCenterTextArray.add(workCenterText);

    }
    chartData.put("textArray", workCenterTextArray); //工作中心
    chartData.put("dateArray", dateArray);//日期横坐标
    chartData.put("dataArray", dataArray);//二维数组
    return ajaxDone(chartData);
  }

}
